/**
 * @Title: ApiCustomerController
 * @Package cn.com.yiqi.wsc.api.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/2/27 14:07
 * @version V1.0
 */
package cn.com.yiqi.wsc.api.controller;

import cn.com.yiqi.wsc.Ro.BalanceRecordVO;
import cn.com.yiqi.wsc.Ro.SpreadVO;
import cn.com.yiqi.wsc.api.controller.ro.CommonRO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;
import cn.com.yiqi.wsc.dao.balance.entity.BalanceFetchEntity;
import cn.com.yiqi.wsc.entity.customer.Customer;
import cn.com.yiqi.wsc.entity.customer.CustomerMention;
import cn.com.yiqi.wsc.enumeration.PointBizType;
import cn.com.yiqi.wsc.enumeration.PointStatus;
import cn.com.yiqi.wsc.service.balance.BalanceService;
import cn.com.yiqi.wsc.service.customer.CustomerService;
import cn.com.yiqi.wsc.service.customer.impl.CartServiceImpl;
import cn.com.yiqi.wsc.service.customer.impl.SignserviceImpl;
import cn.com.yiqi.wsc.service.point.PointService;
import cn.com.yiqi.wsc.service.point.dataobject.QueryPointDO;
import cn.com.yiqi.wsc.service.sysparams.SysParamsService;
import cn.com.yiqi.wsc.Ro.PointParaDO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Calendar;


/**
 *
 *@ClassName: ApiCustomerController
 * Description: TODO
 * @author 会员系统组-林威
 * @date 2017/2/27 14:07
 *
 */
@ResponseBody
@RequestMapping("/api/user")
@Controller
public class ApiCustomerController {

    @Autowired
    CustomerService customerImpl;

    @Autowired
    PointService pointService;

    @Autowired
    BalanceService balanceService;

    @Autowired
    SysParamsService sysParamsService;
    @Autowired
    CartServiceImpl cartService;

    @Autowired
    SignserviceImpl signservice;
    /**
     * 个人中心首页
     */
    @RequestMapping("/account")
    public Result account(@RequestBody CommonRO ro){
        Result re = new Result();
        re.putData("user",customerImpl.selectById(ro.getUserId()));
        re.putData("hasAddress",customerImpl.findAllAddress(ro.getUserId()).size() > 0?true:false);
        return  re;
    }

    /**
     * 发送验证码
     * */
    @RequestMapping("/sendCode")
    public Result sendCode(@RequestBody CommonRO ro){
        Result<Customer> re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re = customerImpl.sendCode(ro.getMobile(),ro.getUserId());
        return re;
    }
    /**
     * 验证验证码绑定手机
     * */
    @RequestMapping("/bindingMobile")
    public Result bingdingMobile(@RequestBody CommonRO ro){
        Result<Customer> re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re = customerImpl.bingMobile(ro.getMobile(),ro.getUserId(),ro.getCode());
        return re;
    }


    /**
     * 我的钱包
     * */
    @RequestMapping("/wallet")
    public Result wallet(@RequestBody CommonRO ro){
        Result<Customer> re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re.putData("balance",re.getReturnValue().getBalance());
        re.putData("point",re.getReturnValue().getPoint());
        return re;
    }

    /**
     * 积分兑换记录
     * */
    @RequestMapping("/pointRecord")
    public Result pointRecord(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        QueryPointDO qdo = new QueryPointDO();
        if(ro.getPageSize() == null){
            ro.setPageSize(10);
            ro.setCurrentPage(1);
        }
        qdo.setPageSize(ro.getPageSize());
        qdo.setCurrentPage(ro.getCurrentPage());
        qdo.setCustomerId(ro.getUserId());
        qdo.setType(PointBizType.exchange_to_balance);
        qdo.setStatus(PointStatus.used);
        re.putData("data",pointService.queryPointRecord(qdo));
        return re;
    }

    /**
     * 积分明细
     * */
    @RequestMapping("/pointList")
    public Result pointList(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }

        //TODO 积分明细
        QueryPointDO qdo = new QueryPointDO();
        if(ro.getPageSize() == null){
            ro.setPageSize(10);
            ro.setCurrentPage(1);
        }
        qdo.setPageSize(ro.getPageSize());
        qdo.setCurrentPage(ro.getCurrentPage());
        qdo.setCustomerId(ro.getUserId());
        qdo.setCustomerId(ro.getUserId());
        re.putData("data",pointService.queryPointRecord(qdo));

        return re;
    }

    /**
     * 积分兑换页面
     * */
    @RequestMapping("/pointExchange")
    public Result pointExchange(@RequestBody CommonRO ro){
        Result<Customer> re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }

        // 1.剩余积分  2.总计兑换积分 3.本月可兑换次数 4 本月可兑换积分
        //账户积分
        PointParaDO pointPara = sysParamsService.getPointPara();
        Integer point = re.getReturnValue().getPoint();
        Long hasExchangedAmoutMonth = customerImpl.hasExchangeAmout(ro.getUserId());
        Integer totaltimesMonth = pointPara.getPoint2balanceTimes();
        Integer totalAmoutMonth = pointPara.getPoint2balanceTotalMax();
        Long  hasExchangedTimesMonth = customerImpl.hasExchangeTimes(ro.getUserId());
        Long  hasExchangedAll = pointService.getPoint2BalanceTotalAmount(Long.parseLong(ro.getUserId()+""),null,null,PointBizType.exchange_to_balance.getValue());
        Long canPointTime = totaltimesMonth - hasExchangedTimesMonth<0?0:totaltimesMonth - hasExchangedTimesMonth;
        Long canPointAmout = 0L;
        Long leftAmount = totalAmoutMonth - hasExchangedAmoutMonth<0?0:totalAmoutMonth - hasExchangedAmoutMonth;
        if(canPointTime <= 0){
            canPointAmout = 0L;
        }else {
            leftAmount = leftAmount > pointPara.getPoint2balancePerMax() ? pointPara.getPoint2balancePerMax() : leftAmount;
            canPointAmout = point > leftAmount ? leftAmount : point;
        }
        re.putData("pointPara",pointPara);
        re.putData("userPoint",point);
        re.putData("hasExchangedAll",hasExchangedAll);
        re.putData("canPointTime",canPointTime);
        re.putData("hasPointTimeMoth",hasExchangedTimesMonth);
        re.putData("hasPointTotalMoth",hasExchangedAmoutMonth);
        re.putData("canPointAmount",canPointAmout);
        re.putData("totalExchanged",hasExchangedAll);
        return ResultUtil.success(re);
    }

    /**
     * 积分兑换余额
     * */
    @RequestMapping("/exchange")
    public Result exchange(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re) || ro.getExchangePoint() == null || ro.getExchangePoint() <= 0){
            return ResultUtil.fail(re,"参数错误");
        }
        // 是否可以兑换
        re = customerImpl.canExchange(ro.getUserId(),ro.getExchangePoint());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        // 余额记录
        re = balanceService.point2Balance(Long.parseLong(ro.getUserId()+""),ro.getExchangePoint());
        return re;
    }

    /**
     * 申请额外兑换
     * */
    @RequestMapping("/extraExchangeApply")
    public Result extraExchangeApply(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re) || ro.getExchangePoint() == null || ro.getExchangePoint() <= 0){
            return ResultUtil.fail(re,"参数错误");
        }
        re = customerImpl.canExtraExchange(ro.getUserId(),ro.getExchangePoint());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re = pointService.extraPointApply(ro.getUserId(),ro.getExchangePoint());

        return re;
    }

    /**
     *额外积分兑换申请记录
     * */
    @RequestMapping("/extraExchangeApplyRecord")
    public Result extraExchangeApplyRecord(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re = pointService.extraPointApplyRecord(ro.getUserId());
        re.putData("scale",sysParamsService.getPointPara().getPointScale());
        return re;
    }

    /**
     * 余额明细
     * */
    @RequestMapping("/balanceRecord")
    public Result balanceRecord(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        List<BalanceRecordVO> list = balanceService.balanceList(ro.getUserId());
        re.putData("list",list);

        return ResultUtil.success(re);
    }

    /**
     * 余额提现申请记录
     * */
    @RequestMapping("/withdrawRecord")
    public Result withdrawRecord(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        List<BalanceFetchEntity> list  = balanceService.selectFetchListByUserId(ro.getUserId());
        re.putData("list",list);
        return re;
    }

    /**
     * 提现页面
     * */
    @RequestMapping("/withDraw")
    public Result withdraw(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }

        return balanceService.withDrawIndex(ro.getUserId());
    }
    /**
     * 申请提现
     *
     * */
    @RequestMapping("/withdrawApply")
    public Result withdrawAply(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re = balanceService.canWithdraw(ro.getUserId(),ro.getBalance());
        return re;
    }

    /**
     * 我的生日提醒
     * */
    @RequestMapping("/mentionList")
    public Result mentionList(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        List<CustomerMention> list = customerImpl.findAllBirthMention(ro.getUserId(),null);
        re.putData("list",list);
        return re;
    }

    /**
     * 新增或修改生日提醒
     * */
    @RequestMapping("/mentionUpdate")
    public Result mentionUpdate(@RequestBody CustomerMention mention){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        if(null != mention.getBithStr()){
            try {
                mention.setBirthday(sf.parse(mention.getBithStr()));
            }catch (Exception e){

            }
        }
        Result re = customerImpl.isExist(mention.getCustomerId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re = customerImpl.insertOrUpdateMention(mention);
        return re;
    }

    /**
     * 生日提醒
     * */
    @RequestMapping("/getMention")
    public Result getmention(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re.putData("mention",customerImpl.findAllBirthMention(null,ro.getId()));
        return re;
    }
    /**
     * 删除生日提醒
     * */
    @RequestMapping("/deleteMention")
    public Result deleteMention(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        customerImpl.deleteMention(ro.getId(),ro.getUserId());
        return ResultUtil.success(re);
    }


    /**
     * 分销记录
     * */
    @RequestMapping("/spreadList")
    public Result spreadList(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        if(ro.getPageSize() == null){
            ro.setPageSize(10);
            ro.setCurrentPage(1);
        }
        PageInfo<SpreadVO> page = customerImpl.findSpread(ro.getUserId(),ro.getPageSize(),ro.getCurrentPage());
        re.putData("page",page);
        return re;
    }

    /**
     * 我的推荐
     * */
    @RequestMapping("/myRecommend")
    public Result myRecommend(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        BigDecimal totalBalance = customerImpl.sumSpreadBalance(ro.getUserId());
        BigDecimal totalPoint = customerImpl.sumSpreadPoint(ro.getUserId());
        PageInfo<Customer> page = customerImpl.findRecomend(ro.getUserId(),1,20);
        re.putData("totalBalance",totalBalance);
        re.putData("totalPoint",totalPoint);
        re.putData("customers",page);
        re.putData("total",page.getTotal());
        return re;
    }

    @RequestMapping("/cartAndSign")
    public Result paraGet(@RequestBody CommonRO ro){
        Result re = customerImpl.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re.putData("cartCount", cartService.findAll(ro.getUserId()).size());
        re.putData("hasSign",signservice.hasSign(ro.getUserId()));
        return re;
    }

}
